import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { AttendanceService } from 'src/app/services/project/smart-site/attendance';
import { Router } from '@angular/router';

@Component({
  selector: 'app-attendance',
  templateUrl: './attendance.component.html',
  styleUrls: ['./attendance.component.scss']
})
export class AttendanceComponent implements OnInit {
  public projectInfo: any = JSON.parse(window.localStorage.getItem('project'));
  public isVisible = false;
  public checkDate = new Date();
  public type = '';
  public dateList = [];
  public detailList = [];
  public detailPageInfo = {
    nzPageIndex: 1,
    maxResultCount: 12,
    nzTotal: 0,
  }

  constructor(
    private router: Router,
    private attendanceService: AttendanceService,
    private message: NzMessageService
  ) { }

  ngOnInit() {
    this.initData();
  }

  routeToSurvey() {
    const projectId = JSON.parse(window.localStorage.getItem('project')).id;
    this.router.navigate([`/inner/project/${projectId}/survey`], {queryParams: { info: 'project' }});
  }

  /**
   * 查询日历信息
   * @param params 
   */
  async handleQueryProjectAttendanceReport(params) {
    const data = {
      projectId: this.projectInfo.smartSiteId,
      ...params
    }
    const res = await this.attendanceService.QueryProjectAttendanceReport(data)
    if (res.result.length) {
      // 求和，放在第一位
      res.result.forEach(element => {
        let sum = 0
        element.array.forEach(item => {
          sum += item
        });
        element.array.splice(0, 0, sum);
      });
    }
    this.dateList = res.result
    // console.log(this.dateList)
  }

  /**
   * 查询考勤详情
   * @param params 
   */
  async handleQueryProjectAttendancePagedList(params) {
    const data = {
      type: this.type,
      date: this.setDateToYYMMDD(this.checkDate),
      maxResultCount: this.detailPageInfo.maxResultCount,
      projectId: this.projectInfo.smartSiteId,
      ...params
    }
    const res = await this.attendanceService.QueryProjectAttendancePagedList(data)
    this.detailList = res.result.items
    this.detailPageInfo.nzTotal = res.result.totalCount
  }

  handlePageIndexChange(index) {
    const params = {
      skipCount: index - 1,
    }
    this.handleQueryProjectAttendancePagedList(params);
  }

  initData() {
    const params = {
      date: this.setDateToYYMMDD(this.checkDate)
    }
    this.handleQueryProjectAttendanceReport(params)
  }

  setDateToYYMMDD(date) {
    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
  }

  onPickerChange(value) {
    let date = this.setDateToYYMMDD(value)
    const params = {
      date
    }
    this.handleQueryProjectAttendanceReport(params)
  }

  onCalendarChange(value: Date): void {
    // console.log(`Current value: ${value}`);
    let date = this.setDateToYYMMDD(value)
    const params = {
      date
    }
    this.handleQueryProjectAttendanceReport(params)
  }

  getDetail(index) {
    // event.stopPropagation()
    // console.log(index)
    // 异步操作防止因事件冒泡导致checkDate字段更新不及时
    setTimeout(() => {
      const data = {
        0: '',
        1: 'JL',
        2: 'SG',
        3: 'ZY'
      }
      this.type = data[index]
      const params = {
        skipCount: 0,
      }
      this.handleQueryProjectAttendancePagedList(params).then(res => {
        if (this.detailList.length) {
          this.isVisible = true
        } else {
          this.message.warning('暂无数据')
        }
      })
    }, 10);
  }

  closeModal() {
    this.isVisible = false
    this.detailList = []
    this.detailPageInfo.nzPageIndex = 1
  }

}
